package com.meinv.auto;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.jdbc.mysql.MysqlLocalConnect;
import com.jdbc.mysql.MysqlServerConnect;
import com.joke.util.Const;
import com.joke.util.DateUtil;
import com.joke.util.DownloadUtil;
import com.joke.util.HttpClientGet;
import com.joke.util.StringUtil;

public class FetchImage {
	private static final Logger logger = LoggerFactory.getLogger(FetchImage.class);

//	private static String neihahImage = "http://www.neihanshequ.com/pic/";
	private static String pengfuGifImage = "http://www.7dapei.com/tuku/page/mimi/(*).html";
	private static String pengfuGifImage1 = "http://www.7dapei.com/tuku/page/qingchun/(*).html";
	private static String pengfuGifImage2 = "http://www.7dapei.com/tuku/page/siwa/(*).html";
	private static String pengfuGifImage3 = "http://www.7dapei.com/tuku/page/pipi/(*).html";
	private static String pengfuGifImage4 = "http://www.7dapei.com/tuku/page/sex/(*).html";
	private static String pengfuGifImage5 = "http://www.7dapei.com/tuku/page/shaofu/(*).html";
	private static String pengfuGifImage6 = "http://www.7dapei.com/tuku/page/av/(*).html";
	private static String pengfuGifImage7 = "http://www.7dapei.com/tuku/page/jiepai/(*).html";
//	private static String pengfuJpgImage = "https://www.pengfu.com/qutu_(*).html";
	
//	private static String nhCookie = "tt_webid=6530444013979846151; uuid=\"w:339f486c971d4499a6ca6e531589d304\"; _ga=GA1.2.2083410321.1520487485; csrftoken=dacecabf09780373b7ce80e7b9efac97; _gid=GA1.2.40257319.1521793305";
	private static String pfCookie = "Hm_lvt_53756750bad0066507bd205b907f59a3=1520563525,1520594801,1520909758,1520994206";


	private Date day = new Date();
	private String date = DateUtil.formatString(day, "yyyyMM");
	private String Y = DateUtil.formatString(day, "yyyy");
	private String M = DateUtil.formatString(day, "MM");
	private String D = DateUtil.formatString(day, "dd");
	private String ymd = Y+"/"+M+"/"+D;
    String writeImagePath="D:/sese/"+date+"入库趣图.txt";
	
	public static void main(String[] args) {
		new FetchImage().fetch();
	}

	public void fetch() {
		Map<String, String> imageMap = new HashMap<>();
//		neihanImage(imageMap);
		pengfuGifImage(imageMap);
		logger.info("抓取的<趣图>条数："+imageMap.size());
		//保存数据库  本地和服务器
/*    	MysqlServerConnect.insertImage(imageMap);*/
    	//保存入库趣图
    	DownloadUtil.writeImage(writeImagePath, imageMap);
		System.out.println(imageMap.size());
		for (String key : imageMap.keySet()) {
			System.out.println(key);
			System.out.println(imageMap.get(key));
		}
	}
	
	private Map<String, String> pengfuGifImage(Map<String, String> imageMap) {
		for (int i = 1; i < 50; i++) {
			String contentHTML = getContent(pengfuGifImage,pfCookie,i);
//			System.out.println(contentHTML);
			parsePFGifImageContent(contentHTML,imageMap);
		}
	/*	for (int i = 1; i < 150; i++) {
			String contentHTML = getContent(pengfuGifImage1,pfCookie,i);
//			System.out.println(contentHTML);
			parsePFGifImageContent(contentHTML,imageMap);
		}
		for (int i = 1; i < 150; i++) {
			String contentHTML = getContent(pengfuGifImage2,pfCookie,i);
//			System.out.println(contentHTML);
			parsePFGifImageContent(contentHTML,imageMap);
		}
		for (int i = 1; i < 150; i++) {
			String contentHTML = getContent(pengfuGifImage3,pfCookie,i);
//			System.out.println(contentHTML);
			parsePFGifImageContent(contentHTML,imageMap);
		}
		for (int i = 1; i < 150; i++) {
			String contentHTML = getContent(pengfuGifImage4,pfCookie,i);
//			System.out.println(contentHTML);
			parsePFGifImageContent(contentHTML,imageMap);
		}
		for (int i = 1; i < 150; i++) {
			String contentHTML = getContent(pengfuGifImage5,pfCookie,i);
//			System.out.println(contentHTML);
			parsePFGifImageContent(contentHTML,imageMap);
		}
		for (int i = 1; i < 150; i++) {
			String contentHTML = getContent(pengfuGifImage6,pfCookie,i);
//			System.out.println(contentHTML);
			parsePFGifImageContent(contentHTML,imageMap);
		}		for (int i = 1; i < 150; i++) {
			String contentHTML = getContent(pengfuGifImage7,pfCookie,i);
//			System.out.println(contentHTML);
			parsePFGifImageContent(contentHTML,imageMap);
		}*/
		return imageMap;
	}

	private String getContent(String url,String cookie, int i) {
		HttpClientGet.createDefaultHttpClient();
		Map<String, String> header = new HashMap<>();
		header.put("Cookie",cookie);
		HttpClientGet.setHeader(header);
		if(i==0) return null;
		else if(i>0) url = url.replace("(*)", String.valueOf(i));
		System.out.println("-----"+url);
		String contentHTML = HttpClientGet.sendHttpGet(url);
		return contentHTML;
	}

	
	private Map<String, String> parsePFGifImageContent(String contentHTML,Map<String, String> imageMap) {
		if(contentHTML==null||"".equals(contentHTML)) return imageMap;
		Document doc = Jsoup.parse(contentHTML);  
		Elements links = doc.select("figure");
		String link1 = links.html();
		
		Document doc1 = Jsoup.parse(link1); 
		Elements links2 = doc1.select("img");
		String imagePath = links2.attr("src");
		String alt = links2.attr("alt");
		
		
		Elements links3 = doc.select("[class=content]");
		String txt = links3.text().trim();
		imageMap.put(imagePath, alt+":::"+txt);
		return imageMap;
	}
	
}
